import os
from textfsm import TextFSM



text = '''
------------------------------------------------
Local Intf: Te1/1/1
Chassis id: 3c51.0ec7.2dd7
Port id: Ethernet3/20
Port Description: L2MON_
System Name: SAS-N9508-CNBJPEK12-01.lenovo.com

System Description:
Cisco Nexus Operating System (NX-OS) Software 7.0(3)I7(9)
TAC support: http://www.cisco.com/tac
Copyright (c) 2002-2020, Cisco Systems, Inc. All rights reserved.

Time remaining: 119 seconds
System Capabilities: B,R
Enabled Capabilities: B,R
Management Addresses:
    Other: 3C 51 0E FF 2D FF 00
Auto Negotiation - not supported
Physical media capabilities - not advertised
Media Attachment Unit type - not advertised
Vlan ID: 1

------------------------------------------------
Local Intf: Te1/1/2
Chassis id: 3c51.0ec7.2d0b
Port id: Ethernet3/20
Port Description: L2MON_
System Name: SAS-N9508-CNBJPEK12-02.lenovo.com

System Description:
Cisco Nexus Operating System (NX-OS) Software 7.0(3)I7(9)
TAC support: http://www.cisco.com/tac
Copyright (c) 2002-2020, Cisco Systems, Inc. All rights reserved.

Time remaining: 100 seconds
System Capabilities: B,R
Enabled Capabilities: B,R
Management Addresses:
    Other: 3C 51 0E FF 2D 0B 00
Auto Negotiation - not supported
Physical media capabilities - not advertised
Media Attachment Unit type - not advertised
Vlan ID: 1

------------------------------------------------
Local Intf: Te1/0/48
Chassis id: a85b.f7c8.2943
Port id: a85b.f7c8.2943
Port Description: eth0
System Name: WAP-L3-211-CNPEK12

System Description:
ArubaOS (MODEL: 555), Version Aruba AP

Time remaining: 96 seconds
System Capabilities: B,W
Enabled Capabilities: W
Management Addresses:
    IP: 10.119.21.76
Auto Negotiation - supported, enabled
Physical media capabilities:
    1000baseT(FD)
    100base-TX(FD)
    Other/unknown
Media Attachment Unit type - not advertised
Vlan ID: 1
PoE+ Power-via-MDI TLV:
 Power Pair: Signal
 Power Class: Class 4
 Power Device Type: Type 2 PD
 Power Source: PSE
 Power Priority: unknown
 Power Requested: 25500 mW
 Power Allocated: 25500 mW

------------------------------------------------

'''

# 正则匹配测试 cisco_3850-16-9_interface_ip
# print (
#     '===正则匹配测试 cisco_3850-16-9_lldp_neighbors==='
#     + '\n' 
#     + __TextFilter(
#         './template/cisco_3850-16-9_interface_ip_template.textfsm',
#         '../vender/cisco/textfsm/cisco_3850-16-9_interface_ip.textfsm'
        
#         )
#     )

# 正则匹配测试 cisco_3850-16-9_lldp_neighbors
# file_path1 = './vender/cisco/textfsm/cisco_3850-16-9_lldp_neighbors.textfsm'
# file_path2 = './textFsmTest/template/cisco_3850-16-9_lldp_neighbors_template.textfsm'

# print(os.path.exists(file_path1))
# print(os.path.exists(file_path2))
# file = open('../vender/cisco/textfsm/cisco_3850-16-9_lldp_neighbors.textfsm')
# 测试 正则匹配输出函数
def TextFilter(_templatePath: str, _text: str) -> list:
    try:
        template = TextFSM(open(_templatePath))
        print(template.header)
        data = template.ParseTextToDicts(_text)
        # data.insert(0, template.header)
        return data
    except Exception as err:
        print(f'发生错误,详情：{err}')

data = TextFilter(
        './vender/cisco/textfsm/cisco_3850-16-9_lldp_neighbors.textfsm',
        text
        )

for row in data:
    print(row)